import Vue from "vue";
import Router from "vue-router";

import { Message } from "element-ui";

Vue.use(Router);

const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};

import Layout from "@/views/layout/index";
import dashboardRouter from "./modules/dashboard";
// import contractRouter from "./modules/contract";
import dynedAccountRouter from "./modules/dynedAccount";
// import customerRouter from "./modules/customer";
import studentRouter from "./modules/student";
// import teacherRouter from "./modules/teacher";
import advertRouter from "./modules/advert";
// import moduleRouter from "./modules/module";
import authorityRouter from "./modules/authority";
import classRoomRouter from "./modules/classroom";
// import courseRouter from "./modules/course";
// import nicheRouter from "./modules/niche";
// import appointRouter from "./modules/appoint";
// import visitRouter from "./modules/visit";
// import contractChangeApplicationRouter from "./modules/contractChangeApplication";
// import studentFollowRouter from "./modules/studentFollow";
// import liveManage from "./modules/liveManage";

import testRouter from "./modules/test";

export const constantRouterMap = [
  {
    path: "",
    hidden: true,
    redirect: "/dashboard"
  },
  dashboardRouter,
  // customerRouter,
  // nicheRouter,
  // appointRouter,
  // visitRouter,
  // contractRouter,
  // studentFollowRouter,
  // courseRouter,
  
  // teacherRouter,
  classRoomRouter,
  studentRouter,
  dynedAccountRouter,
  // contractChangeApplicationRouter,
  authorityRouter,
  advertRouter,
  // moduleRouter,
  // liveManage,
  {
    path: "/login",
    hidden: true,
    component: resolve => require(["@/views/login"], resolve)
  },
  {
    path: "/loginentry/:serial/:id/:tid/:usertype/:centerid",
    hidden: true,
    component: resolve => require(["@/views/loginentry"], resolve)
  },
  {
    path: "/testrole",
    hidden: true,
    component: resolve => require(["@/views/testrole"], resolve)
  },
  {
    path: "/403",
    name: "notAuthority",
    component: () => import("@/views/errorPage/403"),
    hidden: true,
    meta: {
      title: "权限不足"
    }
  },
  {
    path: "*",
    name: "notFound",
    component: () => import("@/views/errorPage/404"),
    hidden: true,
    meta: {
      title: "404"
    }
  }
];

const router = new Router({
  routes: constantRouterMap
});

router.beforeEach((to, from, next) => {
  /* 正式环境 */
  const publicPathList = ["/login", "/loginentry", "/403", "/404", "/testrole"];
  // 无需登录访问路径
  const user = JSON.parse(sessionStorage.getItem("user"));
  //console.log(user)
  if (publicPathList.indexOf(to.path.substring(0, 11)) == -1) {
    // 需要登录才能访问
    if (user) {
      let user = JSON.parse(sessionStorage.getItem("user"));
      if (user.role_id == 1) {
        // 管理员 不加限制
        next();
      } else {
        let rules = JSON.parse(user.rules);
        console.log("rules", rules);
        if (from.path == "/" || from.path == "/login") {
          // 进入左侧导航栏有权限的一页
          let name = "notAuthority";
          for (let i = 0; i < constantRouterMap.length; i++) {
            let flag = false;
            if (constantRouterMap[i].children) {
              for (let j = 0; j < constantRouterMap[i].children.length; j++) {
                for (let api of to.meta.api) {
                  if (rules[api] && rules[api].indexOf("list") != -1) {
                    name = constantRouterMap[i].children[j].name;
                    rules[api].map(item => {
                      if (to.meta.authority.indexOf(item)) {
                        //   next({ name: to.name })
                      }
                    });
                    flag = true;
                    next();
                    break;
                    //
                  }
                }
              }
              if (flag) {
                break;
              }
            }
          }
          //next({ name });
        } else {
          // 其他页面去无权限页面的情况
          for (let api of to.meta.api) {
            if (rules[api] && rules[api].indexOf("list") != -1) {
              let lock = true;
              rules[api].map(item => {
                if (to.meta.authority.indexOf(item) != -1) {
                  lock = false;
                  next();
                }
              });
              if (lock) {
                //next({ name: 'notAuthority' });
                Message({
                  message: `暂无权限！！`,
                  type: "error",
                  showClose: true,
                  duration: 5000
                });
              }
            } else {
              Message({
                message: `暂无权限！！`,
                type: "error",
                showClose: true,
                duration: 5000
              });
            }
          }
        }
      }
    } else {
      next({ path: "/login" });
    }
  } else {
    next();
  }
});

export default router;
